package cn.whdream.datastructure.sort;

import java.awt.print.Printable;

/**
 * 
 * 选择排序
 * 从小到大
 * 
 */
public class SelectSortDemo {
	
	public static void main(String[] args) {
		int[] arr = {119,101,34,1};
		selectSort(arr);
		
	}
	public static void selectSort(int[] arr) {
		int minIndex = 0;
		int min = 0;
		//通过 -1 可以减少一次不必要的循环
		for (int i = 0; i < arr.length -1; i++) {
			minIndex = i;
			min = arr[i];
			for (int j = i+1; j < arr.length; j++) {
				if (min > arr[j]) {
					min = arr[j];
					minIndex = j;
				}
			}
			if (i == minIndex) {
				continue;
			}else {
				arr[minIndex] = arr[i];
				arr[i] = min;
			}
		}
		print(arr);
	}
	private static void print(int[] arr) {
		for (int i : arr) {
			System.out.println(i);
		}
	}
}
